<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends MY_Controller {

    public function index()
    {   
        $data['logged_in']  = $this->logged_in;
        $data['is_admin']   = $this->is_admin;
        $data['first_name'] = $this->first_name;
        
        //Determine next monthly meeting (occurs on 2nd thursday of each month)
        define('MAX_POSSIBLE_SECOND_THURS', 14);
        define('JANUARY', 1);
        define('DECEMBER', 12);
        define('THURSDAY', 4);
		
		define('MAX_TWEETS_TO_DISPLAY', 5);

        $current_month  =   intval(date('n'));
        $next_month     =   $current_month == DECEMBER ? 1 : $current_month + 1;
        $current_day    =   intval(date('j'));

        $iThursdayCount =   0;
        $month_to_use   =   $current_day <= MAX_POSSIBLE_SECOND_THURS ? $current_month : $next_month;
        $year_to_use    =   $month_to_use <> JANUARY ? intval(date('Y')) : intval(date('Y')) + 1;
        for ($iDay = 1; $iDay <= MAX_POSSIBLE_SECOND_THURS; $iDay++) {
                $date_to_check  =   strtotime($year_to_use.'-'.$month_to_use.'-'.$iDay);
                if(date('N', $date_to_check)== THURSDAY) {
                $iThursdayCount++;
                if ($iThursdayCount == 2)
                   break;
            }
        }

        $next_meeting_date = $month_to_use.'/'.$iDay.'/'.$year_to_use;

        
        $data['next_meeting_date'] = $next_meeting_date;
        
        $data['additional_javascript_doc_ready_functions'] = $this->_get_doc_ready_functions();
        $twitter_feed = $this->_get_twitter_feed();
        $data['twitter_feed'] = !$twitter_feed ? 'Can\'t display tweets.  Twitter\'s being difficult.' : $twitter_feed ;
        $data['title'] = "Central Alabama VW Club -- Main";
        $data['heading'] = "Main";
        $data['view'] = 'welcome_message';
        $this->load->vars($data);
        $this->load->view('master_view', $data);
    }
        
    function _get_doc_ready_functions()
    {

        $ret_val    =   '$(\'.slideshow\').cycle({
                         fx: \'fade\', height:\'300px\'
                         });';

        return $ret_val;
    }
	
	//Get the top 5 tweets from clubs twitter feed.
    function _get_twitter_feed()
    {
        $ret_val    =   '';
        
        $url = "https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=CAWVC&page=1";
        $url_headers = @get_headers($url);
        if($url_headers[0] != 'HTTP/1.0 200 OK')
            return false;
        
		$twitter_xml  =  simplexml_load_file('https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=CAWVC&page=1');
		$ret_val = '<br>';
		$tweet_count = 0;
		foreach ($twitter_xml->status as $tweet) {
			//Ignore tweets that reply to another twitter user
			if ($tweet->in_reply_to_user_id == '') {
				$tweet_date_text = new DateTime($tweet->created_at);
				$tweet_date = $tweet_date_text->format('m-d-Y'); 
				
				$tweet_text = $tweet->text;
				
				//http://saturnboy.com/2010/02/parsing-twitter-with-regexp/
				//Links
				$tweet_text = preg_replace(
											'@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@',
											 '<a href="$1">$1</a>',
											$tweet_text);
				
				//Users:
				$tweet_text = preg_replace(
											'/@(\w+)/',
											'<a href="http://twitter.com/$1">@$1</a>',
											$tweet_text);
				
				//Hastags
				$tweet_text = preg_replace(
											'/\s+#(\w+)/',
											' <a href="http://search.twitter.com/search?q=%23$1">#$1</a>',
											$tweet_text);
				
				
				$ret_val .=  '<b><u>'.$tweet_date.'</u></b><br>'.$tweet_text.'<br><br>';
				
				$tweet_count++;
				if ($tweet_count == MAX_TWEETS_TO_DISPLAY)
					break;
			}
		}
		
		$ret_val .='Subscribe to CAWVC on <a target="new_window" href="https://twitter.com/#!/CAWVC">Twitter</a>.';

        return $ret_val;
    }

}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */